/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#pragma once


#include "S_Px车.h"




struct S_车辆场景数据查询 {

	S_车辆场景数据查询();
	~S_车辆场景数据查询();



	static S_车辆场景数据查询* allocate(
		const physx::PxU32 maxNumVehicles,
		const physx::PxU32 maxNumWheelsPerVehicle,
		const physx::PxU32 maxNumHitPointsPerWheel,
		const physx::PxU32 numVehiclesInBatch,
		physx::PxBatchQueryPreFilterShader preFilterShader,
		physx::PxBatchQueryPostFilterShader postFilterShader,
		physx::PxAllocatorCallback& allocator
	);



	//Free allocated buffers.
	void free(physx::PxAllocatorCallback& allocator);

	//Create a PxBatchQuery instance that will be used for a single specified batch.
	//创建将用于单个指定批处理的PxBatchQuery实例。
	//setUpBatchedSceneQuery
	static physx::PxBatchQuery* setUpBatchedSceneQuery(const physx::PxU32 batchId, const S_车辆场景数据查询& vehicleSceneQueryData, physx::PxScene* scene);

	//Return an array of scene query results for a single specified batch.
	//返回单个指定批次的场景查询结果数组。
	physx::PxRaycastQueryResult* getRaycastQueryResultBuffer(const physx::PxU32 batchId);

	//Return an array of scene query results for a single specified batch.
	//返回单个指定批次的场景查询结果数组。
	physx::PxSweepQueryResult* getSweepQueryResultBuffer(const physx::PxU32 batchId);

	//Get the number of scene query results that have been allocated for a single batch.
	//获取已为单个批次分配的场景查询结果的数量。
	physx::PxU32 getQueryResultBufferSize() const;





private:
	//Number of queries per batch
	physx::PxU32 mNumQueriesPerBatch;

	//Number of hit results per query
	physx::PxU32 mNumHitResultsPerQuery;

	//One result for each wheel.
	physx::PxRaycastQueryResult* mRaycastResults;
	physx::PxSweepQueryResult* mSweepResults;

	//One hit for each wheel.
	physx::PxRaycastHit* mRaycastHitBuffer;
	physx::PxSweepHit* mSweepHitBuffer;

	//Filter shader used to filter drivable and non-drivable surfaces
	physx::PxBatchQueryPreFilterShader mPreFilterShader;

	//Filter shader used to reject hit shapes that initially overlap sweeps.
	physx::PxBatchQueryPostFilterShader mPostFilterShader;






	/*PxBatchQuery* f_setUpBatchedSceneQuery(PxScene* scene)
	{
		PxBatchQueryDesc sqDesc(mNbSqResults, 0, 0);
		sqDesc.queryMemory.userRaycastResultBuffer = mSqResults;
		sqDesc.queryMemory.userRaycastTouchBuffer = mSqHitBuffer;
		sqDesc.queryMemory.raycastTouchBufferSize = mNumQueries;
		sqDesc.preFilterShader = mPreFilterShader;
		return scene->createBatchQuery(sqDesc);
	}*/

	//PxVehicleWheelQueryResult mVehicleWheelQueryResults[10];


};




//车辆碰撞过滤
inline physx::PxQueryHitType::Enum WheelSceneQueryPreFilterBlocking(
	physx::PxFilterData filterData0, physx::PxFilterData filterData1,
	const void* constantBlock, physx::PxU32 constantBlockSize,
	physx::PxHitFlags& queryFlags)
{
	//filterData0 is the vehicle suspension query.
	//filterData1 is the shape potentially hit by the query.
	PX_UNUSED(filterData0);
	PX_UNUSED(constantBlock);
	PX_UNUSED(constantBlockSize);
	PX_UNUSED(queryFlags);
	return ((0 == (filterData1.word3 & DRIVABLE_SURFACE)) ? physx::PxQueryHitType::eNONE : physx::PxQueryHitType::eBLOCK);
}





struct S_Px车模拟 {
	S_车辆场景数据查询*		m_车辆查询;
	physx::PxBatchQuery*	m_批量查询;


	void f_初始化车辆解算(S_Px物理引擎核心& Px核心, physx::PxDefaultAllocator& allocator, physx::PxScene* scene);
	void f_模拟(physx::PxFoundation* Px核心, physx::PxScene* scene, S_Px车** 车, uint32 车数量, float32 步进时间);

};





